﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SumiaAccountBook
{
    public static class ExportToFilecs
    {
        public static void Export(string fileName,List<Model.IExporSource> dataList)
        {
            if (null == dataList || dataList.Count<=0)
            {
                return;
            }
            using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (StreamWriter m_streamWriter=new StreamWriter(fs,UTF8Encoding.UTF8))
                {
                    m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);
                    m_streamWriter.WriteLine(dataList.ElementAt(0).GetHeader());                    
                    foreach (var data in dataList)
                    {
                        m_streamWriter.WriteLine(data.GetRow());
                    }
                    m_streamWriter.Flush();
                    m_streamWriter.Close();
                }
            }            
        }
    }
}
